(function(window) {
	window.Inegi = {
		fillStates : function($el) {
			$el.find('option').remove();

			$el.append($('<option value="-1">... Seleccione una opción ...</option>'))

			for (var i = 0; i < Inegi.states.length; i++)
				$el.append($('<option value="' + Inegi.states[i].id + '">' + Inegi.states[i].name + '</option>'));
		},
		getCities : function($el, idState, overlay) {
			if (overlay)
				showOverlay(true);

			var data = {
				m : "gc",
				state : idState
			};

			var call = new Gen.AjaxCall();

			call.get("ServletGeoInfo", data, function(c) {
				$el.find('option').remove();

				$el.append($('<option value="-1">... Seleccione una opción ...</option>'))

				if (c != null) {
					Inegi.cities = c;

					for (var i = 1; i < Inegi.cities.length; i++)
						$el.append($('<option value="' + Inegi.cities[i].clave_municipio + '">'
								+ Inegi.cities[i].nombre_municipio + '</option>'))
				}

				if (overlay)
					hideOverlay();
			}, function(er) {
				if (overlay)
					hideOverlay();
			});
		},
		fillCities : function($el, idState, overlay, callback) {
			if (overlay)
				showOverlay(true);

			var data = {
				state : idState
			};

			var call = new Gen.AjaxCall();

			call.post("ajax/GetCities", data, function(c) {
				$el.find('option').remove();

				$el.append($('<option value="-1">... Seleccione una opción ...</option>'))

				if (c != null) {
					Inegi.cities = JSON.parse(c.cities);

					for (var i = 1; i < Inegi.cities.length; i++)
						$el.append($('<option value="' + Inegi.cities[i].clave_municipio + '">'
								+ Inegi.cities[i].nombre_municipio + '</option>'))
				}

				if (overlay)
					hideOverlay();
				if (callback != null)
					callback();
			}, function(er) {
				if (overlay)
					hideOverlay();
			});
		},
		getTowns : function($el, idState, idCity, overlay) {
			if (overlay)
				showOverlay(true);

			var data = {
				m : "gt",
				state : idState,
				city : idCity
			};

			var call = new Gen.AjaxCall();

			call.get("ServletGeoInfo", data, function(c) {
				$el.find('option').remove();

				$el.append($('<option value="-1">... Seleccione una opción ...</option>'))

				if (c != null) {
					Inegi.towns = c;

					for (var i = 1; i < Inegi.towns.length; i++)
						$el.append($('<option value="' + Inegi.towns[i].clave + '">' + Inegi.towns[i].nombre + '</option>'))
				}

				if (overlay)
					hideOverlay();
			}, function(er) {
				if (overlay)
					hideOverlay();
			});
		},
		fillTowns : function($el, idState, idCity, overlay) {
			if (overlay)
				showOverlay(true);

			var data = {
				state : idState,
				city : idCity
			};

			var call = new Gen.AjaxCall();

			call.post("ajax/GetTowns", data, function(c) {
				$el.find('option').remove();

				$el.append($('<option value="-1">... Seleccione una opción ...</option>'))

				if (c != null) {
					Inegi.towns = JSON.parse(c.towns);

					for (var i = 1; i < Inegi.towns.length; i++)
						$el.append($('<option value="' + Inegi.towns[i].clave + '">' + Inegi.towns[i].nombre + '</option>'))
				}

				if (overlay)
					hideOverlay();
			}, function(er) {
				if (overlay)
					hideOverlay();
			});
		},
		states : [ {
			id : 1,
			name : "Aguascalientes"
		}, {
			id : 2,
			name : "Baja California"
		}, {
			id : 3,
			name : "Baja California Sur"
		}, {
			id : 4,
			name : "Campeche"
		}, {
			id : 5,
			name : "Coahuila de Zaragoza"
		}, {
			id : 6,
			name : "Colima"
		}, {
			id : 7,
			name : "Chiapas"
		}, {
			id : 8,
			name : "Chihuahua"
		}, {
			id : 9,
			name : "Distrito Federal"
		}, {
			id : 10,
			name : "Durango"
		}, {
			id : 11,
			name : "Guanajuato"
		}, {
			id : 12,
			name : "Guerrero"
		}, {
			id : 13,
			name : "Hidalgo"
		}, {
			id : 14,
			name : "Jalisco"
		}, {
			id : 15,
			name : "México"
		}, {
			id : 16,
			name : "Michoacán de Ocampo"
		}, {
			id : 17,
			name : "Morelos"
		}, {
			id : 18,
			name : "Nayarit"
		}, {
			id : 19,
			name : "Nuevo León"
		}, {
			id : 20,
			name : "Oaxaca"
		}, {
			id : 21,
			name : "Puebla"
		}, {
			id : 22,
			name : "Querétaro"
		}, {
			id : 23,
			name : "Quintana Roo"
		}, {
			id : 24,
			name : "San Luis Potosí"
		}, {
			id : 25,
			name : "Sinaloa"
		}, {
			id : 26,
			name : "Sonora"
		}, {
			id : 27,
			name : "Tabasco"
		}, {
			id : 28,
			name : "Tamaulipas"
		}, {
			id : 29,
			name : "Tlaxcala"
		}, {
			id : 30,
			name : "Veracruz de Ignacio de la Llave"
		}, {
			id : 31,
			name : "Yucatán"
		}, {
			id : 32,
			name : "Zacatecas"
		}

		],
		cities : [],
		towns : []

	};
})(window);
